package com.fengkai.filepost.pdfoutstream.test.solutions;


/**
 * @author Fengkai  用数组实现队列
 * @creat 2023/4/4
 */
public class ArrayStack {
    public static void main (String[] args) {
        Statck statck = new Statck(5);
        statck.add(5);
        statck.add(4);
        statck.add(3);
        System.out.println(statck.pol());
        System.out.println(statck.pol());
        System.out.println(statck.pol());
        System.out.println(statck.pol());


    }


}

class Statck {
    int[] datas;
    int pushIndex;//入队坐标
    int pullIndex;//出对坐标
    int size;

    public Statck (int bigArray) {
        this.datas = new int[bigArray];
        pushIndex = 0;
        pullIndex = 0;
        size = 0;
    }

    public void add (int input) {
        size++;
        if (size > datas.length) {
            size--;
            throw new RuntimeException("队列溢出");
        }
        datas[pushIndex] = input;
        pushIndex = nextIndex(pushIndex);
    }

    public int pol () {
        if (size <= 0) {
            throw new RuntimeException("队列为空");
        }
        size--;
        int data = datas[pullIndex];
        pullIndex = nextIndex(pullIndex);
        return data;
    }

    private int nextIndex (int index) {
        return index < datas.length - 1 ? index + 1 : 0;
    }
}
